var util = require('../../libs/util.js');
var app = getApp();
var ringChart = null;
Page({
  data: {
    card:'',
    userInfo: {},
    truename: "",
    hasData: false,
    pay_date: "",
    liter_number:'',
    price_liter:'',
    pay_price:'',
    mile:'',
    picture: "/images/upload-images.png",
    pay_date: util.dateObjectformat(new Date(), 'yyyy-MM-dd')
  },
  onShareAppMessage: function () {
    return {
      title: app.appName,
      path: '/pages/index/index'
    }
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName +"--加油登记" });

    that.setData({
      userInfo: app.globalData.userInfo,
      truename: app.globalData.truename
    });
    if (e.card != 'undefined'){
      that.setData({
        card: e.card
      });
    }else{
      that.setData({
        card: '000000'
      });
    }
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 100
        });
      }
    });
  },
  fromSubmit: function () {
    var that = this;
    wx.showLoading({
      title: '请稍候...',
    });
    var data = {
      card: that.data.card,
      openid: app.globalData.openid,
      pay_date: that.data.pay_date,
      liter_number: that.data.liter_number,
      price_liter: that.data.price_liter,
      pay_price: that.data.pay_price,
      mile: that.data.mile,
      picture: that.data.picture,
      oper_user: app.globalData.driverId
    };
    if (that.data.card == '' || that.data.card == undefined) {
      wx.showToast({
        title: "没有油卡信息",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (app.globalData.driverId == '' || app.globalData.driverId == undefined) {
      wx.showToast({
        title: "没有司机信息",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (that.data.pay_date == '' || that.data.pay_date == undefined) {
      wx.showToast({
        title: "加油日期错误",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (that.data.liter_number == '' || that.data.liter_number == undefined) {
      wx.showToast({
        title: "加油升数错误",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (that.data.price_liter == '' || that.data.price_liter == undefined) {
      wx.showToast({
        title: "每升油价错误",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (that.data.pay_price == '' || that.data.pay_price == undefined) {
      wx.showToast({
        title: "加油金额错误",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (that.data.mile == '' || that.data.mile == undefined) {
      wx.showToast({
        title: "里程数错误",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    if (that.data.picture == '' || that.data.picture == '/images/upload-images.png' || that.data.picture == undefined) {
      wx.showToast({
        title: "没有图片",
        image: '/images/wrong.svg',
        duration: 2000
      });
      return false;
    }
    util.postData("vehicle_gasrecords", data , function (res, error) {
      wx.hideLoading();
      if(error){
        wx.showToast({
          title: res.data.data,
          image: '/images/wrong.svg',
          duration: 3000
        });
        return false;
      }
      console.log(res)
      if (res.data.success) {
        wx.showToast({
          title: res.data.data,
          icon: 'success',
          duration: 3000,
          mask:true,
          success:function(){
            wx.redirectTo({
              url: '/pages/driver/gasrecords'
            });
          }
        });
      }else if (res.data.success == false) {
          wx.showToast({
            title: res.data.data,
            image: '/images/wrong.svg',
            duration: 3000,
            mask: true
          });
      }
    });
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
    if (this.data.liter_number != '' && item == 'price_liter'){
      this.setData({
        pay_price: Number(this.data.liter_number) * Number(e.detail.value)
      });
    }
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var item = String(e.currentTarget.id), item_value = {};
        item_value[item] = res.tempFilePaths[0];
        that.setData(item_value);
        wx.uploadFile({
          url: app.server +'/wechat/wxapplet/uploadCard',
          header:{
            'Authorization': app.globalData.token
          },
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            'id': app.globalData.driverId,
            'type': item
          },
          success: function (res) {
            var data = JSON.parse(res.data); console.log(data.data[0].path)
            if (data.data[0].path) {
              item_value[item] = app.server + data.data[0].path;
              that.setData(item_value);
            }
            //console.log(that.data)
          }
        })
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  gotoDriverHome: function () {
    wx.redirectTo({
      url: '/pages/driver/orderList'
    })
  },
  gotoDriverInfo: function () {
    wx.redirectTo({
      url: '/pages/driver/driverInfo'
    })
  },
  gotoCurrentOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/currentOrder'
    })
  },
  gotoHistoryOrder: function () {
    wx.redirectTo({
      url: '/pages/driver/historyOrder'
    })
  }
})
